﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros_ecWfAletPend : PageBase
{
    private const int ItemsPerRequest = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CarregaContrato(rcbContrato);

            OcultaCampos(true);

            rcbProcesso.Enabled = false;
        }
    }

    #region Eventos
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        Pesquisa();
        RadGrid1.DataBind();
        OcultaCampos(false);
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Pesquisa();
    }
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if ("TableViewWf".Equals(e.Item.OwnerTableView.Name))
        {
            if (e.CommandName == "Select")
            {
                string link = string.Empty;

                GridEditableItem editedItem = e.Item as GridEditableItem;
                int ID = int.Parse(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["IdWfConfig"].ToString());

                TipoProcesso tpProcesso = new TipoProcesso();
                List<TipoProcesso> listTpProcesso = tpProcesso.SelectPorWfConfig(ID);

                foreach (TipoProcesso item in listTpProcesso)
                {
                    if (item.Url != "ProcessoPadrao.aspx")
                        link = "_wf/wf" + item.Url + "?IdWfConfig=" + ID.ToString();
                    else
                    {
                        TipoProcesso documentosTpProcesso = new TipoProcesso(item.IdTipoProcesso, true);
                        link = "_wf/wf" + item.Url + "?IdWfConfig=" + ID.ToString() + "&IdTipoProcesso=" + documentosTpProcesso.IdTipoProcesso.ToString() + "&NomeProcesso=" + documentosTpProcesso.Descricao;
                    }
                    break;
                }

                //Server.Transfer(link);
                Response.Redirect(link);
            }
        }
    }
    protected void rcbContrato_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        rcbProcesso.Enabled = true;
    }
    protected void RadGrid1_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        switch (e.DetailTableView.Name)
        {
            case "TableViewWf":
                {
                    int idProcessoContrato = int.Parse(dataItem.GetDataKeyValue("IdProcessoContrato").ToString());
                    e.DetailTableView.DataSource = CarregaGridDetails(idProcessoContrato);
                    break;
                }
        }
    }
    protected void rcbProcesso_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        int idContrato = int.Parse(rcbContrato.SelectedValue);

        ProcessoContrato processo = new ProcessoContrato();
        List<ProcessoContrato> listProcesso = processo.SelectPor(idContrato, e.Text);

        int itemOffset = e.NumberOfItems;
        int endOffset = Math.Min(itemOffset + ItemsPerRequest, listProcesso.Count);
        e.EndOfItems = endOffset == listProcesso.Count;

        for (int i = itemOffset; i < endOffset; i++)
        {
            rcbProcesso.Items.Add(new RadComboBoxItem(listProcesso[i].TipoProcesso.Descricao, listProcesso[i].IdProcessoContrato.ToString()));
        }

        e.Message = GetStatusMessage(endOffset, listProcesso.Count);

    }
    #endregion

    #region Metodos
    protected List<ProcessoContrato> CarregaProcessos(string nome, int idContrato)
    {
        ProcessoContrato processo = new ProcessoContrato();
        List<ProcessoContrato> listProcesso = processo.SelectPor(idContrato, nome);

        return listProcesso;
    }
    private void Pesquisa()
    {
        RadGrid1.DataSource = CarregaProcessos(rcbProcesso.Text, int.Parse(rcbContrato.SelectedValue));
    }
    protected List<WorkflowConfig> CarregaGridDetails(int idProcessoContrato)
    {
        WorkflowConfig wfConfig = new WorkflowConfig();
        List<WorkflowConfig> listWfConfig = wfConfig.SelectAtivoPor(idProcessoContrato);

        return listWfConfig;
    }
    protected void CarregaContrato(RadComboBox combo)
    {
        try
        {
            Contrato contrato = new Contrato();
            List<Contrato> listContrato = contrato.SelectContratoVigencia(IdPatrocinador, IdPrestador);

            combo.DataTextField = "Descricao";
            combo.DataValueField = "IdContrato";
            combo.DataSource = listContrato;
            combo.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private static string GetStatusMessage(int offset, int total)
    {
        if (total <= 0)
            return "No matches";

        return String.Format("Items <b>1</b>-<b>{0}</b> de <b>{1}</b>", offset, total);
    }
    protected void OcultaCampos(bool oculta)
    {
        trResultado.Visible = !oculta;
    }
    #endregion
}
